<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>应付记录列表</title>
    <script type="text/javascript">
      var newPayableWin;
      var editPayableWin;
      var payableId;
      var payableAuditWin;
      var payableAuditForCancelWin;
      Ext.onReady(function(){
        var vendorTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{number}|{name}', '</div></tpl>');
        var vendorStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/vendor/getAllVendors?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }])
        });

        var vendor =  new Ext.form.ComboBox({
          fieldLabel: '供应商',
          store: vendorStore,
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'vendor_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          //pageSize: 10,
          //hideTrigger: false,
          tpl: vendorTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction: 'all',
          editable:false,
          anchor: '90%',
          selectOnFocus:true
        });

        var beginDate = new Ext.form.DateField({
          fieldLabel: '付款日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'end_date',
          listeners: {
            select: function(field, e){
              //Ext.MessageBox.alert('提示', '提交成功');
            }
          },
          anchor: '100%'
        });
        var endDate = new Ext.form.DateField({
          fieldLabel: '至    ',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'begin_date',
          listeners: {
            select: function(field, e){
              //Ext.MessageBox.alert('提示', '提交成功');
            }
          },
          anchor: '100%'
        });


        var searchButton = new Ext.Button({
          text: '搜索',
          iconCls: 'finding',
          width:70,
          handler: function(){
            payableDS.removeAll();
            payableDS.baseParams.begin_date = beginDate.getRawValue();
            payableDS.baseParams.end_date = endDate.getRawValue();
            payableDS.baseParams.vendor_id = vendor.getValue();
            payableDS.load();
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          labelWidth: 60,
          width: screenWidth-175,
          labelAlign: 'right',
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: screenWidth-190},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .25,
                      layout: 'form',
                      border: false,
                      items: [vendor]
                    }, {
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [beginDate]
                    }, {
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [endDate]
                    },{
                      columnWidth: .1,
                      layout: 'form',
                      border: false,
                      items: [searchButton]
                    }, {
                      columnWidth: .1,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //----------------
        var payableDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize + 10
          },
          proxy: new Ext.data.HttpProxy({
            url: '/payables/getAllPayables'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'amount',
              type: 'float'
            },{
              name: 'new_amount',
              type: 'float'
            }, {
              name: 'action_date'
            },{
              name: 'type_of_payment'
            }, {
              name: 'description'
            }, {
              name: 'auditing_status'
            },{
              name:'audit_opinion'
            }, {
              name: 'auditor_name',
              mapping:'auditor',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.name;
                }
              }
            }, {
              name: 'vendor_name',
              mapping:'vendor.name'
            }, {
              name: 'employee_name',
              mapping:'employee.name'
            }, {
              name: 'purchase_order_number',
              mapping:'purchase_order.order_number'
            }, {
              name: 'sale_auditing_status',
              mapping:'purchase_order.auditing_status'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });

        //生成出库Grid的CM
        var payableSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var payableCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),payableSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '销售订单单号',
              dataIndex: 'purchase_order_number',
              width: 170
            },{
              header: '销售订单状态',
              dataIndex: 'sale_auditing_status',
              width: 90,
              renderer: auditingStatusRender
            },{
              header: '供应商名称',
              dataIndex: 'vendor_name',
              width: 170
            },{
              header: '付款金额',
              dataIndex: 'amount',
              width: 60
            },{
              header: '申请变更付款金额',
              dataIndex: 'new_amount',
              width: 110
            },{
              header: '支付方式',
              dataIndex: 'type_of_payment',
              width: 60
            },{
              header: '付款人',
              dataIndex: 'employee_name',
              width: 60
            },{
              header: '付款日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 75
            },{
              header: '描述',
              dataIndex: 'description',
              width: 180
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 70
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 55
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            }
          ]});

        //生成要订单的产品的Grid
        var payableGrid = new Ext.grid.GridPanel({
          title:'付款记录',
          store: payableDS,
          cm: payableCM,
          sm: payableSM,
          loadMask: true,
          width: screenWidth-175,
          height: 570,
          frame: true,
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize + 10,
            store: payableDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });
        payableDS.load();

        payableGrid.on('rowcontextmenu', function(grid, rowIndex, e){
          payableId = payableDS.getAt(rowIndex).get('id');
          var auditingStatus = payableDS.getAt(rowIndex).get('auditing_status');
          auditForAdd.setDisabled(true);
          auditForCancel.setDisabled(true);
          if (auditingStatus.indexOf('申请新增') > -1 || auditingStatus.indexOf('申请变更') > -1){
            auditForAdd.setDisabled(false);
          }
          if (auditingStatus.indexOf('申请撤消') > -1) {
            auditForCancel.setDisabled(false);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        //############ 订单-右键菜单 ##########
        var auditForAdd = new Ext.menu.Item({
          text: '【审批付款】',
          //iconCls: 'album',
          handler: rightMenuAuditFn
        });

        var auditForCancel = new Ext.menu.Item({
          text: '【审批付款  <font color=red>(撤消)</font>】',
          //iconCls: 'album',
          handler: rightMenuAuditForCancelFn
        });

        function rightMenuAuditFn(){
          payableAuditWin = new Ext.Window({
            layout: 'fit',
            width: 307,
            height: 217,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '付款审批',
            autoLoad: {
              url: "/payables/auditPayable?id="+payableId,
              scripts: true
            }
          });
          payableAuditWin.on("destroy",function(){
            payableDS.reload();
          });
          payableAuditWin.resizable = false;
          payableAuditWin.show();
        }

        function rightMenuAuditForCancelFn(){
          payableAuditForCancelWin = new Ext.Window({
            layout: 'fit',
            width: 305,
            height: 190,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '采购订单审批<font color=red>(撤消)</font>',
            autoLoad: {
              url: "/payables/auditPayableForCancel?id="+payableId,
              scripts: true
            }
          });
          payableAuditForCancelWin.on("destroy",function(){
            payableDS.reload();
          });
          payableAuditForCancelWin.show();
        }
        
        var rightClick = new Ext.menu.Menu({
          items: [auditForAdd,auditForCancel]
        });
        
        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-175,
          height: 650,
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[queryFormPanel,payableGrid]
        });
        workbenchPanel.render("payables_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="payables_workbench_panel"></div>
  </body>
</html>